package com.yz.jdd.module.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseActivity;
import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.Constants;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.base.utils.StringUtil;
import com.yz.jdd.module.main.adapter.OrderTrscationAdapter;
import com.yz.jdd.network.MySubscriber;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.response.Response_OrderTranscationParm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 订单门店筛选 / 语音播报设置门店
 * 19/01/26 jmliu
 */


public class ChooseDeviceSNActivity extends BaseActivity {


    @BindView(R.id.top_back)
    TextView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.select_img)
    ImageView selectImg;
    @BindView(R.id.allshop_tv)
    TextView allshopTv;
    @BindView(R.id.allshop_layout)
    RelativeLayout allshopLayout;
    @BindView(R.id.listView)
    ListView listView;
    OrderTrscationAdapter adapter;
    Response_OrderTranscationParm responseOrderTranscationParm;


    private String shopIdPar;
    List<Response_OrderTranscationParm.QrcodeBindModelsBean> beanList;
    List<Response_OrderTranscationParm.QrcodeBindModelsBean> newBeanList;

    private String deviceName = "全部";
    private String deviceId = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_device_sn);

        deviceName = getIntent().getStringExtra("deviceName");
        deviceId = getIntent().getStringExtra("deviceId");

        ButterKnife.bind(this);
        topTitle.setText("选择设备SN/码ID");
        String jsonStr = PreferencesUtils.getString(ChooseDeviceSNActivity.this, BaseConfigs.STRING_ORDER_CONDITION_SN, "");
        if (!StringUtil.isEmpty(jsonStr)) {
            responseOrderTranscationParm = new Gson().fromJson(jsonStr, Response_OrderTranscationParm.class);
            // 码id + 设备SN
            beanList = new ArrayList<>();
            newBeanList = new ArrayList<>();
            if (responseOrderTranscationParm.getQrcodeBindModels() != null
                    && responseOrderTranscationParm.getQrcodeBindModels().size() > 0) {
                beanList.addAll(responseOrderTranscationParm.getQrcodeBindModels());
                for (int i = 0; i < responseOrderTranscationParm.getPosSn().size(); i++) {
                    beanList.add(new Response_OrderTranscationParm.QrcodeBindModelsBean(responseOrderTranscationParm.getPosSn().get(i), "设备SN", false));
                }
                if ("全部".equals(deviceName)) {
                    selectImg.setVisibility(View.VISIBLE);
                } else {
                    selectImg.setVisibility(View.GONE);
                }

                for (int i = 0; i < beanList.size(); i++) {
                    // beanList.add(new Response_OrderTranscationParm.QrcodeBindModelsBean(responseOrderTranscationParm.getPosSn().get(i), "设备SN", false));
                    if (!StringUtil.isEmpty(beanList.get(i).getQrcodeId()) && !StringUtil.isEmpty(beanList.get(i).getQrcodeName())) {
                        if (!StringUtil.isEmpty(deviceId) && deviceId.equals(beanList.get(i).getQrcodeId())) {
                            beanList.get(i).setSelect(true);
                        } else {
                            beanList.get(i).setSelect(false);
                        }
                        newBeanList.add(beanList.get(i));
                    }
                }


                adapter = new OrderTrscationAdapter(ChooseDeviceSNActivity.this, newBeanList);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Bundle bundle = new Bundle();
                        // 回调给筛选页面
                        Intent intent = new Intent();
                        intent.putExtra("id", newBeanList.get(i).getQrcodeId());
                        intent.putExtra("name", newBeanList.get(i).getQrcodeName());
                        intent.putExtras(bundle);
                        ChooseDeviceSNActivity.this.setResult(Constants.FilterActivityForResultCode_2005, intent);
                        finish();
                    }
                });
            }

        }
    }


//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            resultFilterAct();
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @OnClick({R.id.top_back, R.id.allshop_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_back:
//                if (!isSettingsActivity) {
//                    resultFilterAct();
//                } else {
                finish();
//                }
                break;
            case R.id.allshop_layout:
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                intent.putExtra("id", "");
                intent.putExtra("name", "全部");
                intent.putExtras(bundle);
                ChooseDeviceSNActivity.this.setResult(Constants.FilterActivityForResultCode_2005, intent);
                finish();
                break;
        }
    }

//    private void resultFilterAct() {
//        Bundle bundle = new Bundle();
//        Intent intent = new Intent();
//        intent.putExtra("shopId", shopIdPar);
//        //     intent.putExtra("shopName", shopNameStr);
//        intent.putExtras(bundle);
//        ChooseDeviceSNActivity.this.setResult(Constants.FilterActivityForResultCode, intent);
//        finish();
//    }

}
